# encoding: utf-8

import sys

from config import settings

# from .config import user_name, password
from utils.http import do_request
from urlConfig.urlList import user_login_url, user_register_url


def do_login(user_name, password):
    json_obj = {
        "user_name": user_name,
        "password": password
    }
    response = do_request(method="post", url=user_login_url, json=json_obj)
    # print(response.json())
    if response.status_code == 200:
        resp = response.json()
        settings.jwtToken = resp.get("result", "")
        settings.IsLogined = True
        return True
    else:
        print("登录失败，请重试！")
        return False


def do_logout():
    print("退出中~")
    from config import settings
    settings.IsClosed = True
    sys.exit(0)


def do_register(user_name, password, password2):
    data = {
        "user_name": user_name,
        "password": password,
        "password2": password2
    }
    response = do_request(method="post", url=user_register_url, json=data)
    print(response.text)
